package com.foreknow.view;

import com.foreknow.model.Admin;
import com.foreknow.sercive.AdminService;
import com.foreknow.sercive.Impl.AdminServiceImpl;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("Login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(155, 49, 149, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(155, 108, 149, 21);
        contentPane.add(passwordField);

        JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");
        label.setBounds(100, 52, 54, 15);
        contentPane.add(label);

        JLabel label_1 = new JLabel("   \u5BC6\u7801\uFF1A");
        label_1.setBounds(100, 108, 54, 21);
        contentPane.add(label_1);

        JButton button = new JButton("\u767B\u5F55");
        button.setBounds(61, 168, 93, 23);
        contentPane.add(button);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    AdminService service = new AdminServiceImpl();
                    String name = textField.getText();// 得到输入的用户名
                    String password = new String(passwordField.getPassword());
                    boolean isRight = service.selectAll(name, password);

                    if (isRight) {
                        MainWindow mainw = new MainWindow();
                        Login.this.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null, "用户名密码为空，或者用户名密码错误", "错误",JOptionPane.WARNING_MESSAGE);
                    }

                }
            });

        JButton button_1 = new JButton("\u6CE8\u518C");
        button_1.setBounds(169, 168, 93, 23);
        contentPane.add(button_1);
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Register register =new Register();
                Login.this.dispose();
            }
        });

        JButton button_2 = new JButton("\u53D6\u6D88");
        button_2.setBounds(272, 168, 93, 23);
        contentPane.add(button_2);
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               textField.setText("");
               passwordField.setText("");
            }
        });
        this.setVisible(true);
    }
}
